﻿import { Component, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { RequirementGetAuditEditComponent } from './requirement-get-audit_edit.component'
import { Requirementgetaudit2rejecteditcomponent } from './requirement-get-audit2_reject_edit.component'
import { TestDataDto,UsingFormDetailDto, UsingFormServiceProxy,UsingFormState, UsingFormDetailExeInput } from '@shared/service-proxies/service-proxies';

@Component({
    templateUrl: './requirement-get-audit2.component.html',
    styleUrls: ['./requirement-get-audit.component.less'],
    animations: [appModuleAnimation()]
})
/**
 * 需求领用
 */
export class RequirementGetAudit2Component extends AppComponentBase{
    @ViewChild('requirementGetAuditEditComponent') requirementGetAuditEditComponent: RequirementGetAuditEditComponent;
    @ViewChild('requirementgetaudit2rejecteditcomponent') requirementgetaudit2rejecteditcomponent: Requirementgetaudit2rejecteditcomponent;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    tabObject:{
        filterText:""
    };
    loading = false;
    caches: any = null;
    logs: any = '';
    filterText = '';
    hidden = true;

    constructor(
        injector: Injector,
        private _usingFormServiceProxy: UsingFormServiceProxy) {
        super(injector);
    }
    addTab(usingFormDto: TestDataDto): void {
    }
    clearTab(): void {

    }

    getUsingFormList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._usingFormServiceProxy.getAllUsingDetails(
            2,//state
            undefined,//startuser 领用人 看谁的
            undefined, //currenthandle 谁负责的
            this.filterText,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.items.length;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();

            });

    }
    getState(value):string{
        return UsingFormState[value];
    }

    /**
     * 需求包提交审核
     * @param record 
     */
    toNext(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }
    /**
     * 拒绝
     * @param record
     */
    reject(record: UsingFormDetailDto) {
        // if(record.state == UsingFormState.校验){
        //     this.primengTableHelper.showLoadingIndicator();
        //     var formToDoInput = new UsingFormDetailExeInput();
        //     formToDoInput.id = record.id;
        //     this._usingFormServiceProxy.backToCreation(formToDoInput)
        //         .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
        //             this.getUsingFormList();
        //             this.notify.info(this.l('SavedSuccessfully'));
        //         });
        // }
    }

    //中库
    midWarehouse(record: UsingFormDetailDto) {
        if(record.state == UsingFormState.校验){
            this.primengTableHelper.showLoadingIndicator();
            var formToDoInput = new UsingFormDetailExeInput();
            formToDoInput.id = record.id;
            this._usingFormServiceProxy.toMidWarehouse(formToDoInput)
                .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                    this.getUsingFormList();
                    this.notify.info(this.l('SavedSuccessfully'));
                });
        }
    }

    /**
     * 需求领用出库
     * @param record 
     */
    toExecuting(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }
    /**
     * 完成申请单
     * @param record 
     */
    toFinish(record: UsingFormDetailDto) {
        this.requirementGetAuditEditComponent.show(record.id,record.state);
    }

    getUsingFormStateStateString(state:number):string{
        return UsingFormState[state];
    }
    
    onResize(event): void {
    
    }

    ngOnInit(): void {
        const self = this;
    }
}
